<template>
  <div class="spu">
    <!-- 三级分类 -->
    <Category :scene="scene"/>
    <el-card style="margin: 10px 0;">
      <div v-show="scene == 0">
        <el-button type="primary" icon="Plus" :disabled="!c3Id" @click="addSpu">添加SPU</el-button>
        <el-table border style="margin: 10px 0;" :data="records">
          <el-table-column label="序号" type="index" align="center" width="100"></el-table-column>
          <el-table-column label="SPU名称" prop="spuName"></el-table-column>
          <el-table-column label="SPU描述" show-overflow-tooltip prop="description"></el-table-column>
          <el-table-column label="SPU操作">
            <template #default="{row, $index}">
              <el-button type="primary" size="small" icon="Plus" title="添加SKU" @click="addSku(row)"></el-button>
              <el-button type="warning" size="small" icon="Edit" title="修改SPU" @click="updateSpu(row)"></el-button>
              <el-button type="info" size="small" icon="View" title="查看SKU列表" @click="findSku(row)"></el-button>
              <el-popconfirm width="200"  @confirm="removeSpu(row)" :title="`你确认要删除${row.spuName}吗?`">
                <template #reference>
                  <el-button type="danger" size="small" icon="Delete" title="删除SPU"></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[3,5,7,9]"
          background
          layout=" prev, pager, next, jumper,->, total, sizes,"
          :total="total"
          @size-change="sizeChange"
          @current-change="getHasSpu"
        />
      </div>
      <!-- spufrom -->
      <spuForm ref="spu" v-show="scene == 1" @changeScene="changeScene"/>
      <!-- spufrom -->
      <skuForm ref="sku" v-show="scene == 2" @changeScene="changeScene" />
      <!-- dialog展示sku列表 -->
      <el-dialog v-model="show" title="SKU列表">
        <el-table :data="skuArr">
          <el-table-column label="SKU名字" prop="skuName"></el-table-column>
          <el-table-column label="SKU价格(元)" prop="price"></el-table-column>
          <el-table-column label="SKU重量(克)" prop="weight"></el-table-column>
          <el-table-column label="SKU图片">
            <template #default="{row}">
              <img v-show="row.skuDefaultImg.includes('blob') || !row.skuDefaultImg" src="@/assets/images/no-img.png" style="width: 100px;height: 100px;"/>
              <img v-show="!row.skuDefaultImg.includes('blob') && (row.skuDefaultImg!=='')" :src="row.skuDefaultImg" style="width: 100px;height: 100px;"/>
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, watch, onBeforeUnmount  } from 'vue'
import { storeToRefs } from 'pinia'
import spuForm from './spuForm.vue'
import skuForm from './skuForm.vue'
import { reqRemoveSpu, reqSkuList,reqHasSpu } from '@/api/product/spu'
import type { SkuDataList, Records, SpuData } from '@/api/product/spu/type'
import { useCategoryStore } from '@/store/modules/category'
import { ElMessage } from 'element-plus'

// 场景值控制表格切换 达到禁用select效果
// 0默认 1spuform 2skuform
const scene = ref(0)

// 分页器默认
const pageNo = ref(1)

// 每页展示条数
const pageSize = ref(3)

// 数据总数
const total = ref(0)

// 获取spu数据
const categoryStore = useCategoryStore()
const { c1Id,c2Id,c3Id } = storeToRefs(categoryStore)
const records = ref<Records>([])
const getHasSpu = async () => {
  const res = await reqHasSpu(pageNo.value, pageSize.value, c3Id.value) 
  if(res.code == 200) {
    records.value = res.data.records
    total.value = res.data.total
  }
}

// 监听三级分类变化 请求数据
watch(c3Id, ()=>{
  if(c3Id) {   
    getHasSpu()
  }
})

// 条数改变
const sizeChange = () => {
  pageNo.value = 1
  getHasSpu()
}


// 添加修改spu
const spu = ref()
const addSpu = () => {
  scene.value = 1
  spu.value.initAddSpu(c3Id.value)  
}

const updateSpu = (row: SpuData) => {
  scene.value = 1
  spu.value.initHasSpuData(row)
}

// 子组件绑定的自定义事件 让scene归0
const changeScene = (data:{flag:number, params?:string}) => {
  scene.value = data.flag
  if(data.params == 'add') {
    pageNo.value = 1
    getHasSpu()
  } else {
    getHasSpu()
  }
}

// 添加sku
const sku = ref()
const addSku = (row:SpuData) => {
  scene.value = 2
  sku.value.initSkuData(c1Id.value,c2Id.value,row)
}

// 获取sku列表
const skuArr = ref<SkuDataList>([])
const show = ref(false)
type ns = number|string
const findSku = async (row:SpuData) => {
  const res = await reqSkuList(row.id as ns)
  skuArr.value = res.data
  show.value = true
}


// 删除spu
const removeSpu = async (row:SpuData) => {
  const res = await reqRemoveSpu(row.id as ns)
  if(res.code == 200) {
    ElMessage({
      type: 'success',
      message: '删除成功~'
    })
    if(spu.value.length <= 1) {
      // pageNo.value = pageNo.value-1
      pageNo.value = pageNo.value - 1 <=1?1:pageNo.value - 1
    }
    getHasSpu()
  } else {
    ElMessage({
      type: 'error',
      message: '删除失败~'
    })
  }
}




// 路由跳转 清空
onBeforeUnmount(()=>{
  // 重置仓库数据
  categoryStore.$reset()
})

</script>

<style lang="scss" scoped>

</style>
